package one.interceptor;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;	
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import one.domain.Admin;
import one.domain.Student;
import one.domain.Teacher;

public class Interceptor1 implements HandlerInterceptor {

	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
		if(request.getRequestURI().indexOf("/login")>0){
				return true;
		}
		
		HttpSession session = request.getSession();
		Object  stu =  session.getAttribute("stu");
		Object tea =  session.getAttribute("tea");
		Object adm =  session.getAttribute("adm");
		if(stu!=null||tea!=null||adm!=null){
			return true;
		}
		 PrintWriter out = response.getWriter();  
	        out.println("<html>");      
	        out.println("<script>");      
	        out.println("window.open ('"+request.getContextPath()+"/login2.jsp','_top')");      
	        out.println("</script>");      
	        out.println("</html>");  
		return false;
	}

}
